const { pinyin } = require('pinyin-pro');

function generateRandomString() {
  const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const base = 62; // 字符集大小
  const max = Math.pow(base, 5); // 五位的最大值
  const randomValue = Math.floor(Math.random() * max); // 生成一个随机整数
  let result = '';

  // 将随机整数分解为五位字符
  let remainder = randomValue;
  for (let i = 0; i < 5; i++) {
    const divisor = Math.pow(base, 4 - i);
    const index = Math.floor(remainder / divisor);
    result += charset[index];
    remainder %= divisor; // 更新余数
  }

  return result;
}

function getCustomizeKey(key) {
  return `${pinyin(key.replace(/[^\u4e00-\u9fa5a-zA-Z]+/g, '').slice(0, 5), {
    toneType: 'none',
  }).replace(/\s+/g, '')}-${generateRandomString()}`;
}

module.exports = {
  input: 'src',
  output: '',
  exclude: ['**/node_modules/**/*', '**/dist/**/*', '**/public/**/*'],
  rules: {
    js: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
    },
    ts: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
    },
    cjs: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
    },
    mjs: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
    },
    jsx: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
      functionSnippets: '',
    },
    tsx: {
      caller: '',
      functionName: 'i18n.global.t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: 'import i18n from "./src/locales/i18n"',
      functionSnippets: '',
    },
    vue: {
      caller: '',
      functionNameInTemplate: '$t',
      functionNameInScript: 'window.$t',
      customizeKey: getCustomizeKey,
      // getCustomSlot 函数用于处理国际化文本中的插槽，将插槽值用大括号包裹
      customSlot: function getCustomSlot(slotValue) {
        return `{${slotValue}}`;
      },
      importDeclaration: '',
      tagOrder: ['script', 'template'],
    },
  },
  prettier: { semi: false, singleQuote: false },
  incremental: true,
  skipExtract: false,
  localePath: './src/locales/zhCn.json',
  localeFileType: 'json',
  excelPath: './src/locales.xlsx',
  exportExcel: false,
  skipTranslate: true,
  locales: ['enUs'],
  globalRule: { ignoreMethods: [] },
  adjustKeyMap(allKeyValue) {
    return allKeyValue;
  },
};
